<?php

namespace App\Admin\Controllers;

use App\Models\Book;
use App\Models\DailyBook;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DailyBookController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '书本日报表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DailyBook);
        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('Id'));
        $grid->column('date', __('日期'));
        $grid->column('book_id', __('书籍ID'));
        $grid->column('book.name', __('书籍名称'));
        $grid->column('active_ct', __('访问人数'));
        $grid->column('new_ct', __('新用户数'));
        $grid->column('auth_ct', __('授权人数'));
        $grid->column('pay_ct', __('付费人数'));
        $grid->column('pay_chapter_ct', __('单章节购买人数'));
        $grid->column('pay_all_chapter_ct', __('剩余章节购买人数'));
        $grid->column('member_ct', __('大会员购买人数'));
        $grid->column('income', __('合计收入'));
        //$grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('更新时间'));

        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->date('date', '日期');

            $books = Book::select(['id', 'name'])->get();
            $bookSelect = [];
            foreach($books as $book){
                $bookSelect[$book->id] = $book->name;
            }
            $filter->equal('book_id','书籍')->select($bookSelect);

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DailyBook::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('date', __('Date'));
        $show->field('book_id', __('Book id'));
        $show->field('active_ct', __('Active ct'));
        $show->field('new_ct', __('New ct'));
        $show->field('auth_ct', __('Auth ct'));
        $show->field('pay_ct', __('Pay ct'));
        $show->field('pay_chapter_ct', __('Pay chapter ct'));
        $show->field('pay_all_chapter_ct', __('Pay all chapter ct'));
        $show->field('member_ct', __('Member ct'));
        $show->field('income', __('Income'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new DailyBook);

        $form->date('date', __('Date'))->default(date('Y-m-d'));
        $form->number('book_id', __('Book id'));
        $form->number('active_ct', __('Active ct'));
        $form->number('new_ct', __('New ct'));
        $form->number('auth_ct', __('Auth ct'));
        $form->number('pay_ct', __('Pay ct'));
        $form->number('pay_chapter_ct', __('Pay chapter ct'));
        $form->number('pay_all_chapter_ct', __('Pay all chapter ct'));
        $form->number('member_ct', __('Member ct'));
        $form->number('income', __('Income'));

        return $form;
    }
}
